package bestTeam.WalkieDoggie.client.Composites;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;

import bestTeam.WalkieDoggie.client.WalkieDoggie;
import bestTeam.WalkieDoggie.client.dateComperator;
import bestTeam.WalkieDoggie.client.Serializables.UserProfileData;
import bestTeam.WalkieDoggie.client.Serializables.UserRequest;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.StackPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MyToDoComp extends Composite {

	private ArrayList<UserRequest> requests;

	private dateComperator date_comperator_asc=new dateComperator(1);
	
	public ArrayList<UserRequest> getRequests() {
		return requests;
	}

	private Label title;
	private VerticalPanel ver;
	private StackPanel stackToDoList;
	private Label emptyLabel;

	public MyToDoComp(HashMap<UserRequest,UserProfileData> requestsMap){
		if(WalkieDoggie.requestsOwnersMap!=null){
			UserRequest[] tmp = (UserRequest[])WalkieDoggie.requestsOwnersMap.keySet().toArray(new UserRequest[0]);
			requests = getMyToDoRequests(tmp);
		}


		
		
		ver = new VerticalPanel();
		
		HorizontalPanel hor = new HorizontalPanel();
		title = new Label("My ToDo Walkies:");
		title.setStyleName("titleForView");
		hor.add(title);
		Label exp = new Label("\t shows walkies that you've volunteered and have been approved");
		hor.add(exp);
		
		hor.setCellVerticalAlignment(exp, HasVerticalAlignment.ALIGN_BOTTOM);
		
		ver.add(hor);
		
		ver.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		ver.setSpacing(5);
		initWidget(ver);
		ver.setSize("100%", "100%");

		stackToDoList = new StackPanel();
		//ver.add(title);
		ver.setCellWidth(title, "100%");
		ver.setCellHeight(title, "15px");
		ver.setCellHorizontalAlignment(title, HasHorizontalAlignment.ALIGN_LEFT);
		ver.add(stackToDoList);
		stackToDoList.setSize("100%", "100%");

		emptyLabel = new Label("no items to show");
		ver.add(emptyLabel);
		
		if(!WalkieDoggie.loginInfo.isNewUser())
			fillWalkies();
	
	}


	private ArrayList<UserRequest> getMyToDoRequests(UserRequest[] requestsArray) {
		ArrayList<UserRequest> res = new ArrayList<UserRequest>();

		for (UserRequest userRequest : requestsArray) {

			if( userRequest.getApprovedWalkerID()!=null &&
				userRequest.getApprovedWalkerID().equals(WalkieDoggie.loginInfo.getUserData().getId()) &&
				userRequest.getDate().after(new Date()))
				
				res.add(userRequest);
		}

		return res;
	}

	private void fillWalkies() {
		boolean f = false;
		
		UserRequest[] sorted = (UserRequest[])requests.toArray(new UserRequest[0]); 
		Arrays.sort(sorted,date_comperator_asc);
		
		for (UserRequest request : sorted) {

			if(request.getApprovedWalkerID().equals(WalkieDoggie.loginInfo.getUserData().getId())){
				String title = WalkieDoggie.fmt.format(request.getDate())+"\t"+request.getTimeFrame();
				stackToDoList.add(new RequestOfFriend(request,false), title);
				f = true;
			}
		}
		if(f)
			emptyLabel.setText("");

	}
}
